/* 
Copyright 2005-2009, Foundations of Success, Bethesda, Maryland 
(on behalf of the Conservation Measures Partnership, "CMP") and 
Beneficent Technology, Inc. ("Benetech"), Palo Alto, California. 

This file is part of Miradi

Miradi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3, 
as published by the Free Software Foundation.

Miradi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Miradi.  If not, see <http://www.gnu.org/licenses/>. 
*/ 

package org.miradi.diagram.coalescer;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.miradi.objects.DiagramFactor;
import org.miradi.objects.DiagramLink;

class Group implements Comparable<Group>
{

	public Group(int direction, final Set<Node> nodes)
	{
		this.nodes = (null == nodes) ? new HashSet<Node>() : nodes;
		this.direction = direction;
	}

	public Collection<DiagramFactor> getDiagramFactors()
	{
		Set<DiagramFactor> factors = new HashSet<DiagramFactor>(nodes.size());
		for (Node node : nodes) {
			factors.add(node.getFactor());
		}
		return factors;
	}
	
	public Integer size()
	{
		return nodes.size();
	}
	
	public Set<Node> getNodes()
	{
		return nodes;
	}
	
	public int getDirection()
	{
		return direction;
	}

	public void remove(Node node)
	{
		nodes.remove(node);
	}

	public void captureNodes()
	{
		if (nodes.size() > 1) 
		{
			for (Node node : nodes)
				node.setGroup(this);
		}
	}
	
	@Override
	public int compareTo(Group other)
	{
		if (this==other) return 0;
		
		if (null==other) return 1;
		
		if (this.direction == other.direction)  
			return this.size().compareTo(other.size());
		else
			return DiagramLink.FROM==this.direction ? 1 : -1;
	}	

	private final Set<Node> nodes;
	private final int direction;

}